/**
 * @ Description   :  接收告警数据发到钉钉机器人
 * @ Author        :  annyliang
 * @ CreateDate    :  2022/8/6 10:30
 * @ UpdateUser    :  annyliang
 * @ Version       :  v1.0
 */
package controllers

import (
	"fmt"
	"learn-api/constant"
	"learn-api/models"
)

// DingDingController
type DingDingController struct {
	BaseController
}

// @Title Get
// @Success 200 "发送成功"
// @Param   DingDing query string
// @Param   Metric query string
// @Param   MetricAlarm query string
// @Failure 400 Param check failed
// @Failure 500 post DingDing common error
// @router / [Get]
func (c *DingDingController) Get() {
	rspm := make(map[string]interface{})
	var dingTalkModel models.DingTalkModel
	dingDing := c.GetString("DingDing")
	if(dingDing != "" && dingDing == "alarm"){
		dingTalkModel.Metric = c.GetString("Metric")
		dingTalkModel.MetricAlarm = c.GetString("MetricAlarm")
		if err := models.SendDingtalk(&dingTalkModel); err == nil {
			rspm[constant.RSP_CODE] = constant.APP_RSP_SUCCESS
			rspm[constant.RSP_INFO] = "钉钉告警信息发送成功!"
			fmt.Println("钉钉告警信息发送成功!")
		} else {
			rspm[constant.RSP_CODE] = constant.APP_RSP_FAIL
			rspm[constant.RSP_INFO] ="钉钉告警信息发送失败"
			fmt.Println("钉钉告警信息发送失败")
		}
	}else {
		rspm[constant.RSP_CODE] = constant.APP_RSP_FAIL
		rspm[constant.RSP_INFO] ="参数为空或不正确"
		fmt.Println("参数为空或不正确")
	}
	c.Data["json"]=rspm
	c.Ctx.Output.SetStatus(200)
	c.ServeJSON()
}
